package com.library.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.library.common.entity.MyPage;
import com.library.common.utils.PageUtil;
import com.library.entity.FeedBack;
import com.library.entity.ResourceAudit;
import com.library.entity.UserInfo;
import com.library.entity.dto.FeedBackDto;
import com.library.mapper.FeedBackMapper;
import com.library.service.FeedBackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.library.service.UserInfoService;
import com.library.util.MoJiCommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 反馈信息表 服务实现类
 * </p>
 *
 * @author ccl
 * @since 2025-02-19
 */
@Service
public class FeedBackServiceImpl extends ServiceImpl<FeedBackMapper, FeedBack> implements FeedBackService {

    @Autowired
    private UserInfoService userInfoService;
    @Override
    public MyPage<FeedBack> queryPage(FeedBackDto dto) {
        LambdaQueryWrapper<FeedBack> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(FeedBack::getCreatedTime);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<FeedBack> feedBacks=this.list(wrapper);
        PageInfo<FeedBack> info = new PageInfo<>(feedBacks);
        return PageUtil.PageInfo2MyPage(info);
    }

    @Override
    public FeedBack detail(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("id不能为空,请核查!");
        }
        return this.getById(id);
    }

    @Override
    public boolean add(FeedBack feedBack) {
        feedBack.setCreatedTime(new Date());
        feedBack.setUpdatedTime(new Date());
        LambdaQueryWrapper<UserInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getOpenId,feedBack.getOpenId());
        UserInfo userInfo=userInfoService.getOne(wrapper);
        if(userInfo !=null){
            feedBack.setProvince("41");
            feedBack.setCity(userInfo.getCityCode());
            feedBack.setArea(userInfo.getAreaCode());
            feedBack.setStreet(userInfo.getStreetCode());
            feedBack.setCommittee(userInfo.getCommitteeCode());
            feedBack.setCreatedBy(userInfo.getNick());
            feedBack.setUpdatedBy(userInfo.getNick());
        }
        return this.save(feedBack);
    }

    @Override
    public boolean edit(FeedBack feedBack) {
        feedBack.setUpdatedTime(new Date());
        feedBack.setUpdatedBy(MoJiCommonUtils.getUserId());
        return this.updateById(feedBack);
    }

    @Override
    public boolean delete(Long id) {
        if(ObjectUtil.isEmpty(id)){
            throw new RuntimeException("id不能为空,请核查!");
        }
        return this.removeById(id);
    }
}
